// eslint-disable-next-line @typescript-eslint/no-var-requires
const BaseController = require('../core/base');
import { Post, Prefix } from 'egg-shell-decorators';
/**
 * @controller User
 */
@Prefix('/api')
export default class HomeController extends BaseController {
  /**
     * @summary 注册新用户
     * @description path 
     * @router post /api/register
     * @request body createUserRequest *body
     */
  @Post('/register')
  public async register({ body: { name, password } }) {
    const { ctx, app } = this

    ctx.validate({
      name: { type: 'name', required: true },
      password: { type: 'string', required: true },
    }, ctx.request.body)
    const token = app.jwt.sign({ name: name }, app.config.jwt.secret);
    const data = ctx.service.test.register(name, password, token);
    ctx.body = await data;
  }
  /**
    * @summary 登录用户
    * @description path 
    * @router post /api/login
    * @request body createUserRequest *body
    */
  @Post('/login')
  public async login({ body: { name, password } }) {
    const { ctx } = this

    ctx.validate({
      name: { type: 'name', required: true },
      password: { type: 'string', required: true },
    }, ctx.request.body)
    ctx.body = await ctx.service.test.login(name, password);;
  }
  /**
    * @summary 查询用户信息
    * @description path 
    * @router post /api/userinfo 
    * @request body userinfo *body
    */
  @Post('/userinfo')
  public async logins({ body: { userId } }) {
    const { ctx } = this
    ctx.body = await ctx.service.test.userinfo(userId);
  }
}
